Docker: From Setup to First Container

4 min December 06, 2017

How to start with Docker? How to install Docker? How to build Docker image? How to run Docker container? That's are the major problems in the beginnings with Docker and this article will hopefully help you to solve them. I will try to explain you how to create your first Dockerfile, build the first image with custom content and how to run, stop and remove the first Docker container.

Docker: From Setup to First Container

Docker Installation

Links on the original Docker documentation: Mac OS, Debian, Ubuntu, CentOs, Windows

I would recommend to install docker-compose. It is a great tool for managing multiple containers and if you plan to use Docker for your projects, you will use it a lot.

After successful installation you will have the docker and docker-compose command available in your console.

Creating the Dockerfile

First step is to create a custom directory for example playground with a Dockerfile (file named Dockerfile without any extension) containing the following piece of code.

FROM php:apache

This code says "Install PHP with Apache". You are probably wondering, where did I get the php:apache. It is from the official php repository on the Docker hub and if you open the link, you will see that there are multiple tags/versions of the repository. The one I chose is the apache version. In a simpler way, when you want to install some repository it can be the basic and default repository, for example FROM php or with a tag FROM php:apache. It can be also php:alpine, php:cli, php:fpm and so on.

For better understanding the Docker image, imagine it like an empty box on which you are putting another boxes.

The php:apache image is build on the debian:stretch-slim image and the debian:stretch-slim image is build on the modified scratch image that uses the basic scratch image (and that is the first empty box).

If you saw all those RUN, ENV, WORKDIR commands in the Dockerfiles after opening the links, don't think about that for now, I will explain them later.

The next step is to create an index.php file with "Hello world!" text inside and add

COPY index.php /var/www/html

into the Dockerfile. This command copies the index.php file into your container.

The last step in this part is to add


on the end of the Dockerfile. This opens the port 80 and allows other containers or you (if you bind external port) to access the container.

The Dockerfile should now looks like this:

FROM php:apache

COPY index.php /var/www/html


Building the Image

We have created our first Dockerfile and it is time to build it. Open command line, move CLI into the directory with the Dockerfile and run

docker build -t hello-world .

This executes the Docker build command for the actual directory (the dot on the end) and creates a hello-world image. If the image was successfully build, you will see a similar output message "Successfully built a6a297113c3f; Successfully tagged hello-world:latest". and after running "docker images" command you should see the hello-world image in the list.

Running the Container

A container is a runtime instance of an image. To run the hello-world image use the following command

docker run -p 80:80 --name hello-world-test hello-world

-p 80:80: sets both ports (internal and external) to 80. The external port is the first number and it is the port for accessing the container. The second number is for the internal port and it must be the same on which is the hello-world container listening (EXPOSE 80).

--name hello-world-test: assigns a name to the container, so the name of the container will be hello-world-test. If you don't assign a name to the container, Docker will add some random name like a keen_benz or friendly_mccarthy.

If the container started successfully, you should be able to access the container from http://localhost and if you run "docker ps" command, you will see running container named hello-world-test.


Running container can be stopped by

docker stop hello-world-test

and removed by

docker rm hello-world-test

With the image it is even simpler. In order to remove it run

docker rmi hello-world

(before removing any Docker image you always must stop all containers that uses the image).