Deploy an app with Docker
Install Docker, run a container with ports and volumes, then orchestrate with docker-compose.
Deploy an app with Docker
Docker isolates your application and its dependencies in a reproducible container. You run an image, map ports and volumes, and you are done.
Cause / The problem
“It works on my machine” is the classic problem: a different runtime version, a missing system dependency, a port already taken. Docker removes these gaps by packaging the app with its environment.
Solution
- Install Docker on your instance:
curl -fsSL https://get.docker.com | sh sudo usermod -aG docker $USER # then log out and back in - Run an existing image:
docker run -d --name my-app -p 8080:8080 -e PORT=8080 my-image:latest-d: detached,--name: container name.-p host:container: map a port,-e KEY=value: an env var.-v /host/path:/container/path: a persistent volume.
- Check the status:
docker ps # running containers docker logs -f my-app # live logs - Build your own image with a
Dockerfile, then:docker build -t my-app . docker run -d -p 8080:8080 my-app - Use docker-compose to manage several services.
docker-compose.yml:
Thenservices: app: image: my-app:latest ports: ["8080:8080"] environment: PORT: "8080" volumes: ["./data:/app/data"] restart: unless-stoppeddocker compose up -d. - Pick a restart policy:
restart: unless-stoppedorrestart: alwaysto survive a reboot. - Update:
docker compose pull && docker compose up -d
Common errors: bind: address already in use (a port is taken — change the host port), permission denied (add your user to the docker group or use sudo).