- Pull image from DockerHub:
docker pull [imageID]
- Run image in detached mode along with a command:
docker run -d [imageID] [command]
- Attatch a running container:
docker attatch [containerID]
- Show Running Containers:
docker ps
- Show All Containers:
docker ps -a
- Kill running container:
docker stop [ContainerID/Name]
- Remove container:
docker rm [ContainerID/Name]
- Show stored images:
docker images
- Remove stored images:
docker rmi [imageID/Name]
- Execute a command on a running container:
docker exec [containerID] [command]
- Run a specific version of an image:
docker run [imageID:tag]
- Run interactive mode:
docker run -i [containerID]
- Run interactive mode and attatch terminal:
docker run -it
- Run image with an environment variable and assign container name:
docker run -e [variableName]=[value] --name [containerName]
- Port mapping:
docker run -p [externalPort]:[internalPort] [containerID]
- Volume mapping:
docker run -v [targetDir]:[internalDir] [containerID]
- Inspect Conainer as JSON:
docker inspect [containerID]
- Container logs:
docker logs [containerID]
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python3 python3-pip
RUN pip install flask
RUN pip install flask-mysql
COPY . /opt/app.py
ENTRYPOINT ["FLASK_APP=/opt/app.py", "flask", "run", "--host=0.0.0.0"]
- Build Image:
docker build Dockerfile -t username/[myApp]
docker push username/[myApp]
- From the directory containing the following
docker-compose.yml
file:
docker compose up [OPTIONS] [SERVICE...]
# version is now using "compose spec"
# v2 and v3 are now combined!
# docker-compose v1.27+ required
services:
vote:
build: ./vote
# use python rather than gunicorn for local dev
command: python app.py
depends_on:
redis:
condition: service_healthy
volumes:
- ./vote:/app
ports:
- "5000:80"
networks:
- front-tier
- back-tier
result:
build: ./result
# use nodemon rather than node for local dev
command: nodemon server.js
depends_on:
db:
condition: service_healthy
volumes:
- ./result:/app
ports:
- "5001:80"
- "5858:5858"
networks:
- front-tier
- back-tier
worker:
build:
context: ./worker
depends_on:
redis:
condition: service_healthy
db:
condition: service_healthy
networks:
- back-tier
redis:
image: redis:5.0-alpine3.10
volumes:
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/redis.sh
interval: "5s"
ports: ["6379"]
networks:
- back-tier
db:
image: postgres:9.4
environment:
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
volumes:
- "db-data:/var/lib/postgresql/data"
- "./healthchecks:/healthchecks"
healthcheck:
test: /healthchecks/postgres.sh
interval: "5s"
networks:
- back-tier
volumes:
db-data:
networks:
front-tier:
back-tier:
- Show docker networks:
docker network ls
- Inspect docker network:
docker network inspect [NetworkType]
- Run a container named alpine-2 using the alpine image and attach it to the none network:
docker run --name alpine-2 --network=none alpine
- Create a new network named wp-mysql-network using the bridge driver:
docker network create --driver bridge --subnet 182.18.0.1/24 --gateway 182.18.0.1 wp-mysql-network
- Inspect the created network:
docker network inspect wp-mysql-network
- Deploy a mysql database using the mysql:5.6 image and name it mysql-db. Attach it to the newly created network wp-mysql-network. Set the database password to use db_pass123. The environment variable to set is MYSQL_ROOT_PASSWORD:
docker run -d -e MYSQL_ROOT_PASSWORD=db_pass123 --name mysql-db --network wp-mysql-network mysql:5.6
- Deploy a web application named webapp using the kodekloud/simple-webapp-mysql image. Expose the port to 38080 on the host:
docker run --network=wp-mysql-network -e DB_Host=mysql-db -e DB_Password=db_pass123 -p 38080:8080 --name webapp --link mysql-db:mysql-db -d kodekloud/simple-webapp-mysql