Introduction to Docker - Part 2

Introduction to Docker - Part 2

Welcome to part 2 of my Introduction to Docker blog post series! In part 1 I quickly went through installing docker and what containers are. If you've not read the previous post I encourage you to do so:

In this post I'll show you how to start the working with containers! We will pull an image from the DockerHub, we will run a container, stop it, destroy it and etc.

I'll be using DigitalOcean for all of the demos, so I would strongly encourage you to create a DigitalOcean account follow along. You would learn more by doing!

To make things even better you can use my referral link to get a free $50 credit that you could use to deploy your virtual machines and test the guide yourself on a few Digital Ocean servers:

Digital Ocean $50 Free Credit

Once you have your account here's how to deploy your first droplet/server:

I'll be using Ubuntu 18.04 so I would recommend that you stick to the same so you could follow along.

Running a container

Once you have your Ubuntu Droplet ready, login and follow along!

So let's run a our first container:

docker run hello-world

You would see a similar output:

Docker Run hello-world

We just ran a container based on the hello-world image, as we did not have the image locally, docker pulled the image from the DockerHub ( and then used that image to run the container. All what happened was, the container ran, printed some text on the screen and then exited.

Then to see some information about the running and the stopped containers run:

docker ps -a

You should see the following information for your hello-world container that you just ran:

[email protected]:~# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
62d360207d08        hello-world         "/hello"            5 minutes ago       Exited (0) 5 minutes ago                       focused_cartwright

Then to list the locally available images on your host run:

docker images

Let's run a more useful container like an Apache container for example. First we can pull the image from the docker hub with the docker pull command

docker pull webdevops/php-apache

docker pull php and apache

Then we can get the image ID with the docker images command:

docker images

The output would look something like this:

docker images

After that we can use the docker run command again:

docker run -d -p 80:80 IMAGE-ID

Quick rundown of the arguments that I've used:

-d - it specifies that I want to run the container in the background. Tat way when you close your terminal the container would remain running.

-p 80:80 - this means that the traffic from the host on port 80 would be forwarded to the container. That way you could access the apache instance which is running inside your docker container directly via your browser

With the docker info command now we can see that we have 1 running container:

docker run image

And with the docker ps command we could see some useful information about the container like the container ID, when the container was started and etc:

[email protected]:~# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                   NAMES
7dd1d512b50e        fd4f7e58ef4b        "/entrypoint supervi…"   About a minute ago   Up About a minute   443/tcp,>80/tcp, 9000/tcp   pedantic_murdock

Then you can stop the running container with the docker stop command followed by the container ID:

docker stop id

If you need to you can start the container again:

docker start container-id

If you need to attach to the container and run some commands inside the container sue the docker exec command:

docker exec -it <container id> /bin/bash 

That way you will get to a bash shell in the container and execute some commands inside the container itself. Then to detach from the interactive shell press CTRL+PQ that way you will not stop the container.

docker exec

To delete the container run first make sure that the container is not running and then run:

docker rm id

If you would like to delete the container and the image all together, just run:

docker rmi [ID]


That is pretty much it! Now you  know how to pull images, run, stop and start containers!

We are ready to move to Part 3 of the Introduction to Docker blog post series:

Let me know if you have any questions!