20 Nov 2018:
A short introduction about the most important docker commands/functionality which might be useful for others.
Installing Docker
To install the community edition of docker on Ubuntu:
sudo apt-get remove docker docker-engine docker.io
sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
Running Docker containers
Here is a tutorial article about Docker with Ubuntu which is very helpful to understand the basics of docker.
Some commands:
docker ps
- List all running Docker containersdocker container ls --all
- List all containers that have been created locallydocker rm <name>
- Use the name (from the command above) to delete a container.docker stop <name>
- As the command saysdocker run -p 4000:80 <image>
- Forward port 4000 on the host to port 80 on the guest
Building your own
To start your own Docker image you need to create a Dockerfile
. This
contains all steps needed. E.g. (taken from the official Docker
documentation):
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
If you, like me, like to put the Docker files all in their own subdirectory this command can be used to build the image:
docker build -f docker/Dockerfile -t <some-name-for-dockerimage> .
Investigate whats wrong at a certain build step. You can inspect the last one by using the last succeeded layers id
docker run --rm -it 829d658fa3b8 bash --login
829d658fa3b8
is the id shown in the last successful build step.
Running commands in a specific container
When the container is still running you can run commands in it by using
docker exec <container-name> <command>
If the last process exits from a container, the container will be
stopped and enter the stopped state. Then it is not possible anymore
to run commands in it. To rerun a container you need to make its state
fixed and known to docker. You can do this with the commit
subcommand:
docker commit <container-id>
You can find the container id with docker container ls --all
It will then create a new container state and output its layer id. You
can use the same run command as above when creating and inspecting the
build of the Dockerfile
:
docker run -it 829d658fa3b8 bash --login
Substitute 829d658fa3b8
with the output ID from the commit
command.
This time we left the --rm
away, so the container is not deleted on
exit and can be used to create a new layer with commit
.
Mounting folders as Volumes from the Host
When running a container you can bind a folder from the host computer
to a folder in the container with -v
. The format is as follows
-v <localfolder>:<guestfolder>
So e.g. with
-v "$PWD":/build
you will mount the current directory from the host to /build
in the
container. This Option can be passed with the run
subcommand.
Saving and loading for deployment
Once you are satisfied with a container you developed on your machine, you can save it to a file for deployment on some infrastructure.
docker save IMAGE_NAME | gzip > IMAGE_NAME.tgz
Loading from a file goes analoguous in reverse with load:
docker load < IMAGE_NAME.tgz
decompression is done and detected automatically.
Cleanup
Here some helpful commands for cleaning up after playing around:
Normal maintenance
docker container prune
- Remove all stopped containersdocker image prune -a
- Remove all images which are not used by a container
Just remove everything
docker stop $(docker ps -a -q)
- Stop all docker containersdocker rm $(docker ps -a -q)
- Remove all docker containersdocker rmi -f $(docker images -q)
- Force remove all docker images