Useful/Common Docker Commands

By | October 16, 2019

Sample Dockerfile

FROM python:2.7-alpine

RUN mkdir /app
WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

LABEL maintainer="Kevin Fatkin <[email protected]>" \
      version="1.0"

VOLUME ["/app/public"]

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

CMD flask run --host=0.0.0.0 --port=5000

Most used image build sample commands

docker image ls

docker image build -t web1 .

docker image tag web1 kfatkin/web1:latest

docker image push kfatkin/web1

docker image rm -f [image id]

docker pull kfatkin/web1

docker image pull redis:3.2-alpine

Most used container running sample commands

docker container ls

docker container inspect web1

docker container run -it -p 5000:5000 -e FLASK_APP=app.py web1

docker container ls -a

docker container rm [container id]

docker container run -it -p 5000:5000 -e FLASK_APP=app.py \
 --rm --name web1 web1

docker container run -it -p 5000:5000 -e FLASK_APP=app.py \
 --rm --name web1 -d web1

docker container logs web1

docker container logs -f web1

docker container stats

docker container run -it -p 5000 -e FLASK_APP=app.py \
 --rm --name web1_2 -d --restart on-failure web1

docker container run -it -p 5000:5000 -e FLASK_APP=app.py \
  -e FLASK_DEBUG=1 --rm --name web1 -v $PWD:/app web1

docker run --rm -itd -p 6379:6379 --name redis redis:3.2-alpine

docker container stop web1

Most used networking sample commands

docker network ls

docker network inspect bridge

docker exec web2 ifconfig

docker exec web2 ping 172.17.0.3

docker exec redis cat /etc/hosts

docker network create --driver bridge samplenetwork

docker network inspect samplenetwork

docker container run --rm -itd -p 6379:6379 --name redis \
 --net firstnetwork redis:3.2-alpine

docker container run --rm -itd -p 5000:5000 -e FLASK_APP=app.py \
 -e FLASK_DEBUG=1 --name web2 --net samplenetwork -v $PWD:/app web2

docker exec web2 ping redis

Common data persistence sample commands

docker volume ls

docker volume create web2_redis

docker volume inspect web2_redis

ocker container run --rm -itd -p 6379:6379 --name redis \
 --net samplenetwork -v web2_redis:/data redis:3.2-alpine

docker exec redis redis-cli SAVE

Sharing data between containers sample commands

docker container run --rm -itd -p 5000:5000 -e FLASK_APP=app.py \
  -e FLASK_DEBUG=1 --name web2 --net samplenetwork -v $PWD:/app web2

docker container run --rm -itd -p 6379:6379 --name redis \
  --net samplenetwork --volumes-from web2 redis:3.2-alpine

docker exec -it redis sh

docker container run --rm -itd -p 5000:5000 -e FLASK_APP=app.py \
  -e FLASK_DEBUG=1 --name web2 --net samplenetwork -v $PWD:/app -v /app/public web2

docker container run --rm -itd -p 6379:6379 --name redis \
  --net samplenetwork --volumes-from web2 redis:3.2-alpine

Entrypoint sample commands (for script running on container start)

docker container run --rm -itd -p 6379:6379 --name redis \
  --net samplenetwork redis:3.2-alpine

docker image build -t webentrypoint .

docker container run --rm -it -p 5000:5000 -e FLASK_APP=app.py \
  -e FLASK_DEBUG=1 --name webentrypoint --net samplenetwork \
  -v $PWD:/app webentrypoint

docker container run --rm -it -p 5000:5000 -e FLASK_APP=app.py \
  -e FLASK_DEBUG=1 \
  -e WEB2_COUNTER_MSG="Docker fans have visited this page" \
  --name webentrypoint --net samplenetwork webentrypoint

Docker cleanup commands

docker container ls -a

docker system df -v

docker system info

docker system prune -f

docker system prune -a

docker container stop $(docker container ls -a -q)

docker-compose.yaml sample file

version: "3"

services:
  vote:
    build: ./vote
    command: python app.py
    volumes:
     - ./vote:/app
    ports:
      - "5000:80"
    networks:
      - front-tier
      - back-tier

  result:
    build: ./result
    command: nodemon --debug server.js
    volumes:
      - ./result:/app
    ports:
      - "5001:80"
    networks:
      - front-tier
      - back-tier

  worker:
    build: ./worker
    networks:
      - back-tier

  redis:
    image: 'redis:3.2-alpine'
    ports: ["6379"]
    networks:
      - back-tier

  db:
    image: postgres:9.4
    volumes:
      - "db-data:/var/lib/postgresql/data"
    networks:
      - back-tier

volumes:
  db-data:

networks:
  front-tier:
  back-tier:

Sample docker-compose commands

docker-compose build

docker-compose pull

docker-compose up

docker-compose stop

docker-compose up --build -d

docker-compose ps

docker-compose logs -f

docker-compose restart

docker-compose restart redis

docker-compose exec web ls -la

docker-compose exec web sh

docker-compose run redis redis-server --version

docker-compose up redis

docker-compose rm

To run Docker interactively if entrypoint fails, first comment out the Dockerfile entries where the failure occurred then tail -f /dev/null to keep container alive when it starts

FROM maven:3.6.3-amazoncorretto-8 as BUILD
COPY . /usr/src/app
ARG CODEARTIFACT_AUTH_TOKEN=nothing_set
COPY settings.xml /usr/share/maven/ref/
RUN cat /usr/share/maven/ref/settings.xml
# RUN mvn --settings /usr/share/maven/ref/settings.xml -X -Dmaven.repo.local=/root/m2 --batch-mode -f /usr/src/app/pom.xml clean package -DskipTests
# FROM openjdk:8-jre-slim
# EXPOSE 8080 5005
# COPY --from=BUILD /usr/src/app/zuora-api/target /opt/target

# WORKDIR /opt/target
# ENV _JAVA_OPTIONS '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'
# ENTRYPOINT ["java","-jar","zuora-api-1.0.jar"]
ENTRYPOINT ["tail", "-f", "/dev/null"]

Next, build the image with proper tags and run it interactive. Once the container is running, execute the shell commands on it.

#Build the container image (run from local code directory)
docker build -t TagNameForImage .

# Run the container
# -d to detach from container
# -it to run interactively
# -v to map local working directory to container directory
# -p to map ports
# -rm can be included to remove container when it's stopped
docker container run -d -it -p 8080:5005 --name NameOfRunningContainer -v $PWD:/usr/src/app TagNameFromBuildStep

# Confirm it's running with container list
docker container ls

# Execute bash shell on container (note, depending on container
# version this might not be bash)
docker container exec -it DockerContainerName /bin/bash