Deploying Docker Compose EC2

TODO: collapasable, copyable, download, etc

I have a docker-compose.yml. I want to deploy to an EC2 host. I have the .pem file which I would connect to with ssh -i my.pem [email protected]. Briefly, how do I format the DOCKER_HOST environment variable and is that the best way to deploy to a host?To deploy your Docker Compose setup on an EC2 host, follow these steps:

  1. First, install Docker and Docker Compose on your EC2 instance. You can follow the official installation guides for Docker (https://docs.docker.com/engine/install/) and Docker Compose (https://docs.docker.com/compose/install/).

  2. Transfer your docker-compose.yml and any other necessary files (e.g., .env, Dockerfile) to the EC2 instance using scp:

scp -i my.pem docker-compose.yml [email protected]:/home/ec2-user/
  1. SSH into the EC2 instance:
ssh -i my.pem [email protected]
  1. Navigate to the directory where you've transferred your docker-compose.yml file and run docker-compose up -d to start your containers in detached mode:
cd /home/ec2-user/
docker-compose up -d

You don't need to set the DOCKER_HOST environment variable in this case. The DOCKER_HOST variable is used to connect your local Docker client to a remote Docker daemon, which is not necessary in this situation. You are directly running the Docker Compose command on the EC2 instance.

If you want to manage the Docker containers on the EC2 instance from your local machine, then you can configure the DOCKER_HOST environment variable:

  1. On the EC2 instance, edit the /etc/docker/daemon.json file (create it if it doesn't exist), and add the following content to allow the Docker daemon to listen on a TCP socket:
{
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"]
}
  1. Restart the Docker service on the EC2 instance:
sudo systemctl restart docker
  1. On your local machine, set the DOCKER_HOST environment variable to point to the EC2 instance:
export DOCKER_HOST="tcp://ec2-54-160-180-240.compute-1.amazonaws.com:2376"
  1. Now you can use Docker commands locally, and they will be executed on the EC2 instance. Note that exposing the Docker daemon over the network without encryption and authentication is not secure. Consider setting up TLS for secure communication (https://docs.docker.com/engine/security/https/).

Deploying Docker Compose EC2 created at Tue Jan 20 1970 11:07:37 GMT+0000 (Coordinated Universal Time)

Related Chats

Deploying Docker Compose EC2 0.999

Update Docker Compose Envvars 0.635

Env File for Docker 0.582

Scaling Docker Across EC2 0.556

Docker ENV Variables Not Set 0.491

Identifying Host Deploying Node 0.459

Dockerfile for MySQL 8.0.34 0.445

Dockerfile for PostgreSQL 16.1 0.427

Switch to EKS: Steps 0.402

Nohup com env em shell 0.394