Skip to content

Instantly share code, notes, and snippets.

@xshapira
Forked from ShawnClake/docker.md
Created July 6, 2021 06:50
Show Gist options
  • Save xshapira/39e844a6c38e73fe2e9f027f59469c0a to your computer and use it in GitHub Desktop.
Save xshapira/39e844a6c38e73fe2e9f027f59469c0a to your computer and use it in GitHub Desktop.
Docker cheat sheet

Docker Cheat Sheet

Helpful Links

Troubleshooting

  • Docker login problem with X11 GUI stuff
    • Taken from: docker/compose#6023
    • I'm having the same (or a similar) issue, running docker login failing with the error message Cannot autolaunch D-Bus without X11 $DISPLAY. Having stumbled across docker/docker-credential-helpers#60 it seems as if golang-docker-credential-helpers causes the issue here. Temporarily uninstalling docker-compose including the package above allowed me to run docker login to authenticate with my docker registry.

List Docker CLI commands

docker docker container --help

Display Docker version and info

docker --version
docker version
docker info

Execute Docker image

docker run hello-world

List Docker images

docker image ls

List Docker containers (running, all, all in quiet mode)

docker container ls
docker container ls --all
docker container ls -aq

Getting a container launched

docker build -t friendlyhello .  # Create image using this directory's Dockerfile

docker build --no-cache -t friendlyhello .  # Create image using this directory's Dockerfile without using the cache

docker run -p 4000:80 friendlyhello  # Run "friendlyname" mapping port 4000 to 80

docker run -d -p 4000:80 friendlyhello         # Same thing, but in detached mode

docker container ls                                # List all running containers

docker container ls -a             # List all containers, even those not running

docker container stop <hash>           # Gracefully stop the specified container

docker container kill <hash>         # Force shutdown of the specified container

docker container rm <hash>        # Remove specified container from this machine

docker image ls -a                             # List all images on this machine

docker image rm <image id>            # Remove specified image from this machine

Docker hub

  • Login
    • docker login
  • Tag a release
    • docker tag image username/repository:tag For example: docker tag friendlyhello gordon/get-started:part2
    • docker tag apitax-docker-2.1.4 nopatience/middleware:2.1.4
  • Push a release
    • docker push username/repository:tag
    • docker push nopatience/middleware:2.1.4
  • Pull a release and build - prioritizes local over pulling from docker hub
    • docker run -p 4000:80 username/repository:tag
    • docker run -p 5080:80 nopatience/middleware:2.1.4

Automatic reboot of containers

  • Add a restart flag to the run command
    • docker run -d --restart unless-stopped -p 5080:80 nopatience/middleware:2.1.4

Example Docker file

# Use an official Python runtime as a parent image
FROM ubuntu:18.04

# install our dependencies and nodejs
RUN apt-get update
RUN apt-get -y install nodejs
RUN apt-get -y install npm

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN apt-get -y install python3 python3-pip
RUN pip3 install --trusted-host pypi.python.org -r requirements.txt

RUN cd /usr/local/lib/python3.6/dist-packages/apitax/ah/web/node/node && npm install && npm run build

WORKDIR /app

# Make port 80 available to the world outside this container
EXPOSE 5080

# Define environment variable
CMD ["python3", "Middleware/middleware.py", "--web", "--debug", "--no-build"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment