How to create a container with Docker

Let’s continue with our guides dedicated to Docker. Today we’ll take a detailed look at a topic that is really fundamental for anyone involved in web development: Docker containers.

A container is the running instance of an application in an isolated environment that is dynamically allocated resources to run. Inside it, the container has the application to run and everything it needs: runtime, libraries, configuration, etc.

What is the difference between an image and a container?

A docker image is a static package containing all the resources necessary to run an application.

Multiple containers can be launched from a Docker image. For example, suppose our application requires multiple instances of a certain service, such as redis. It is possible to have multiple containers in which redis is run from the same redis image available on the Docker hub.

How do you create a container?

To create a container, it is very simple to begin with a starting image: just run the command docker run.

$ docker run redis –name ‘redis-server’

By running this command, docker will download the image from the docker hub and run a redis instance.

The –name argument allows us to identify the container and we can use it as a reference, as we will see in the following examples.

Interacting with the container

You can execute a command inside the container with the docker exec command, as shown in the example:

$ docker exec -it redis-server redis-cli

With this Docker command, we launch the redis-cli command inside the container. Using the -it argument, the command is run in interactive mode: this allows you to access the command prompt inside the container.

Docker Container


Depending on the purpose of the application, the container may display a TCP or UDP port to be accessed from the outside. If we want to make the container communicate with the host, we will have to “map” the port so that Docker makes it available for access by the host.

For example, if on our local computer we have a client with a graphical interface for redis and we want to make it connect to the container, we will have to display port 6379. To do this, when we launch the container, we will have to use the -p argument.

$ docker run redis -p 6379:6379 –name ‘redis-server’

In this way, on we can set as host and 6379 as port our redis client and then access the data contained in the container. We can also specify a custom port for the container; all this comes in handy when you have two containers with the same application, or which in any case display the same port.

$ docker run redis -p 1234:6379 –name ‘redis-server’

To conclude with Docker container

Creating a container with Docker is very simple, as we have seen. Modern developers today must know all Docker functions to perfection if they want to optimise their way of working and the quality of the applications they intend to create.

After this in-depth study dedicated to Docker containers, in the next articles we will look at how to create a working environment with Docker by managing multiple containers and making them interact with each other. These are key topics for those who want to become an advanced level web developer.

Read related articles