Following on from my recent posts about how to install Docker CE, and looking at Docker storage drivers, I thought it was time to dive into doing what Docker is for – and go into how you can use the docker run command to start up some docker containers! This post will be a general overview of how to run Docker containers, with plenty of practical examples, so hopefully this should be a good one!
Starting a Container using Docker Run
Executing containers is done by using the docker run command. A typical example is the Docker hello-world container.
$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Status: Downloaded newer image for hello-world:latest Hello from Docker! ...
When we run the command, the Docker image is pulled from the online Docker image repository, as I am running this image for the first time, and is executed. We can see it works, as the ‘Hello from Docker!’ message is printed to the screen. The image has now been downloaded locally, so if we were to run it again (creating a new container from the image), it would read from the local image.
We can view images local stored on our docker host to system by running the docker images list command:
$ docker images list REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 7 months ago 1.84kB
Run a Detached Docker Container
Moving on, next I’ll try running a container from a busybox image – this time specifying that we want it to be detached (e.g not interactive) by using the -d flag.
Docker run -d busybox
If you’re following along, notice that the image downloads, but then nothing seems to happen. You’re just returned to the CLI prompt. In the background however, a busybox container did indeed get created, it’s just that it exited immediately as we didn’t give it anything to do.
We can see the container ran then exited by running the docker ps -a command:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 06545d3e4811 busybox "sh" 15 seconds ago Exited (0) 14 seconds ago wizardly_ritchie
Notice that the output above shows that the container ran ‘sh’. This time, I’ll run a busybox container, but have it echo some text back to the screen.
$ docker run busybox echo "This is my Busybox Container" This is my Busybox Container
This time, the echo’d text is displayed, before we are returned to the CLI prompt. Docker created the busybox container, ran the command we specified, then exited the container once the work was done.
Run an Interactive Docker Container
Rather than have the container exit, we can run the container interactively, by getting it to run a bash shell.
So far, we’ve ran containers that have either ran in the background or have ran a single command and exited (the hello world example earlier). Another way to run a container is interactively. To do so you can use the -it flag, which attaches us to a virtual tty in the container. For example, keeping with the busybox image, we can do this using the -it flag:
$ docker run -it busybox /bin/bash
Now when the container runs, we will be dropped into a shell session inside the container.
How to List Running Docker Containers
So, now we’ve created a bunch of containers, we can use the ‘docker ps’ command to view them. To view running containers we can use ‘docker ps’, however we need to use ‘docker ps -a’ to view all containers, as all the ones we have ran so far have exited:
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 277bc6be3b1d busybox "echo 'This is my Bu…" 4 minutes ago Exited (0) 4 minutes ago angry_lamport d9fee079e8dd busybox "sh" 8 minutes ago Exited (0) 8 minutes ago inspiring_banach 110a6108d840 hello-world "/hello" About an hour ago Exited (0) About an hour ago upbeat_shockley
As you can see from the ‘STATUS’ column, all of the containers have a status of ‘Exited’.
How to Remove Docker Containers That Are No Longer Running
A useful way to quickly remove all containers with a status of ‘exited’ is to use the following:
docker rm $(docker ps -a -q -f status=exited)
Alternatively, you can use the prune command:
docker container prune
Assigning Container Names using Docker Run
Also note that each container has been allocated an interesting name. If we wanted to assign a new container a name, rather than use a generated one, we can use the –name flag.
Docker run --name my_busybox_container busybox
Now when I run ‘docker ps’ there is a container with my chosen name:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b6dfb8fe1455 busybox "sh" 7 seconds ago Exited (0) 4 seconds ago my_busybox_container
Assigning Memory Limits using Docker Run
Along with the ‘name’ flag I used above, there are a whole bunch of other flags you can use when running a container, which allow you to do things like set the amount of memory assigned to a container, expose network ports and much more. A full list of flags can be found here, but as a quick example, take a look at the following:
docker run -d --name nginx-test --restart unless-stopped -p 8080:80 --memory 500M nginx
The above command will launch a container from the ‘nginx’ image and the container will be called nginx-test. If the container is stopped it will automatically restart, 500mb of memory will be set as a limit, and port 8080 on the local host will be forwarded to port 80 on the container.
Stopping and Starting Docker Containers
The last thing I wanted to mention here is how to stop a container, and how to start a stopped container. This is done using the stop and start commands:
Docker container stop nginx-test Docker container start nginx-test
So, that’s it for this post – a general overview of running Docker containers. There will be upcoming posts that go into running containers in more detail, particularly around the options we have around networking and storage, as I continue to work through the Docker DCA objectives.