DEV Community

Fred Munjogu
Fred Munjogu

Posted on

Understanding Docker, Dockerfile, and Docker Compose

What is Docker?

Docker is an open-source platform that allows one to automate the deployment of applications inside containers. A container is a stand-alone unit that includes all components an application needs to run (code, libraries, and dependencies).

Containers are different from virtual machines in that they share the host system's kernel. This makes them more efficient, resource-wise, and they are a lot easier to manage.

Some of the benefits of using Docker include:

  • Portability
  • Efficiency
  • Scalability
  • Isolation

What is a Dockerfile

Now that we have an idea of what Docker is, we can move to a Dockerfile. A Dockerfile is a text document that contains a set of instructions used in building an image.

An image refers to the blueprint of libraries and dependencies that are necessary inside a container for an application to run.

Docker will read our Dockerfile and build an image that matches the specification listed. To understand this a bit more, let us write one and see what exactly happens.

Using a code editor of your choice, create a file and name it Dockerfile. Inside the file, write the following:

FROM python:3.12-slim

WORKDIR /app

COPY requirements.txt /app

RUN pip install -r /app/requirements.txt

COPY . /app

CMD["python", "main.py"]
Enter fullscreen mode Exit fullscreen mode

As you may have guessed from the file above, we require to have a requirements.txt file and a main.py file. For this part, we will not write anything complex since this is focused on understanding containers and images. So in our requirements.txt file, we will write the following:

requests
Enter fullscreen mode Exit fullscreen mode

In our main.py, we can write a simple "Hello, world!" program.

print("Hello, world!")
Enter fullscreen mode Exit fullscreen mode

With these files now, we can build our first image. To build an image, we use the docker build command. The flag "-t" refers to the tag, which is a name you can assign to your image. If you do not assign a name, one will be assigned by default. The trailing period in the command refers to the relative path of the Dockerfile, which in our case is the current directory.

docker build -t test .
Output:

Results of building the image

To confirm if the image has been built, use docker images and look for an image with the name "test".

Now that our image is built, we will create a container so that we can run our Python file. To create a container, we will use this command:
docker run --name test_container test
Output:

Running the container
Our Python file runs great!

What is Docker Compose?

Docker Compose is a tool used to define and manage multi-container Docker applications. Suppose our project requires Python and a PostgreSQL Database. With Docker Compose, we can define and manage these services in a YAML file. Let us create this YAML file.
Back in your code editor, create a new file docker-compose.yml. Inside the file we will create our services:

services:
  web:
    build: .
    ports:
      - "5000:5000"
    depends_on:
      - db

  db:
    image: postgres:14
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: mydatabase
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:
Enter fullscreen mode Exit fullscreen mode

In this file, we have two services: web and our database. The web service will run our DockerFile, which we specified above. The db service defines our Postgres Database and also the username, password, and name of the database. Volumes ensure data consistency and persistence. This way, when we stop and start the container again, all the data that was in the database will be preserved.

To run this, we will use the command docker compose up. If we want to stop it, we will use docker compose down. A tip when running docker compose up is to use the "-d" flag, which will give you your terminal back.

Top comments (0)