Executing multiple commands in Docker-Compose
Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-container applications.
You can use docker-compose to define instructions to manage services that depend on multiple Docker containers.
In addition to being able to manage multiple containers, docker-compose provides other benefits such as isolated environments for application services and preservation of volume data when creating containers.
Executing multiple commands in Docker-Compose
Docker-compose runs on the Docker Engine and is installed with Docker on Mac and Windows machines. The following command allows you to verify that docker-compose is set up correctly on your machine.
$ docker-compose version
Docker Compose version v2.2.3
In the following example, we will discuss how to set up a Django, PostgreSQL application using docker-compose. We will discuss how to execute multiple commands in docker-compose.
First, we will create a simple Dockerfile with simple entries as shown below. This file outlines the commands that will be executed to create our Docker image.
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /tonyloi
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /tonyloi/
The Dockerfile defines Python 3 as the base image; however, we also define a requirements.txt file to describe other requirements needed for the project to run.
Django>=3.0,<4.0
psycopg2>=2.8
Now that you have these two files ready, you can create the docker-compose.yml file in the root directory of this project. This file describes the application services we intend to build: the database and the web server.
In addition to this, we will also define the Docker images that these services use and any other Docker volumes you may wish to mount. Finally, we also need to specify the ports that the services will expose.
Since this is a Django application, we must also specify the command that will allow us to run migrations and start the development server. There are several ways to do this, using either sh or bash.
In this case, we will use sh since it is available on Unix-based systems.
docker-compose.yml:
version: "3.9"
services:
DB:
image: Postgres
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: >
sh -c "
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8080"
volumes:
- .:/tonyloi
ports:
- "8080:8080"
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
depends_on:
- DB
We will run the following docker-compose run
command to create this Django project. Before executing this command, you must make sure that you are in the root directory.
If not, use the cd command to navigate to the root directory.
~/django_app$ sudo docker-compose run web Django-admin startproject new_app .
Output:
Successfully built 00a4c8d65c91
Successfully tagged django_app_web:latest
WARNING: Image for service web was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating django_app_db_1 ... done
Creating django_app_web_run ... done
This command uses docker-compose and the Docker file to create a Django project. In this example, we name the project new_app.
You can now navigate to the project and list the project files.
isaac@DESKTOP-HV44HT6:~/django_app$ cd new_app
isaac@DESKTOP-HV44HT6:~/django_app/new_app$ ls
__init__.py asgi.py settings.py urls.py wsgi.py
isaac@DESKTOP-HV44HT6:~/django_app/new_app$
This article focuses on how to use sh to execute multiple commands in a docker-compose file. Alternatively, you can also use bash -c
as shown below.
command: bash -c "
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8080
"
We can finish creating the rest of our Django application by configuring our database settings in the settings.py file.
Django uses SQLite by default when developing; therefore, since we are using PostgreSQL, we need to make the necessary configuration below. Be sure to replace the database and username with your own.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('postgres'),
'USER': os.environ.get('postgres'),
'PASSWORD': os.environ.get('enter_your database'),
'HOST': 'db',
'PORT': 5432,
}
}
Finally, to start, the application executes the command in the project's main directory sudo docker-compose up
. It starts the development server at http://0.0.0.0:8080/, as we specified in the docker-compose file.
~/django_app$ sudo docker-compose up
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
View logs for a specific Docker Compose service
Publish Date:2025/03/24 Views:60 Category:Docker
-
When using docker-compose up, we can see the logs of all containers in the YAML file; however, if we specify a specific container service, the output will not show any service dependencies in the log. Therefore, this article will explore ho
Differences between Docker and Docker Compose
Publish Date:2025/03/24 Views:146 Category:Docker
-
Both docker and docker compose docker run Docker containers. The two Docker constructs are comparable, but that's about it. This article will discuss docker compose the main differences between docker and . Differences between Docker and Do
Recreate the container from the new image using Docker-Compose
Publish Date:2025/03/24 Views:178 Category:Docker
-
As we develop our applications, we often make changes or add more features to make the application more effective for the different users who interact with the application. When using Docker, we need to ensure that the changes or features w
Exposing multiple ports in a Docker container
Publish Date:2025/03/24 Views:186 Category:Docker
-
There are different types of communication on the Internet, the most common ones include file transfers, sending emails, and serving web pages. To make this communication possible, we make use of port numbers that help identify the type of
Using Docker network host commands
Publish Date:2025/03/24 Views:51 Category:Docker
-
Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking. When we create containers without specifying a network, they ar
Clear Docker container logs
Publish Date:2025/03/24 Views:192 Category:Docker
-
Logs are information recorded by the application when a specific event or state occurs. They help us monitor the application and take necessary actions. For example, when we deploy an application to a production environment, logs can help u
Docker daemon log location
Publish Date:2025/03/24 Views:65 Category:Docker
-
The Docker daemon provides essential information about the general state of your microservices architecture. Unfortunately, container-centric logging techniques allow you to collect relevant data from your services but provide little inform
Difference between COPY and ADD commands in Dockerfile
Publish Date:2025/03/24 Views:140 Category:Docker
-
A Dockerfile is a text document that contains all the commands used to build a Docker image. Recently, we have seen Docker being widely used as the default tool for managing configurations and automating deployments. Advanced features such
The --rm flag in Docker
Publish Date:2025/03/24 Views:151 Category:Docker
-
Typically, when most developers start using docker, after going through all the processes of pulling images, building them, and running containers, removing a container defeats the purpose of doing so. However, for experienced developers, t