Zach's Mugspideyclick logo

GitHub

GitLab

Linkedin

Instagram

Youtube

SoundCloud

Email

Docker Cheat Sheet

https://github.com/wsargent/docker-cheat-sheet

https://docker-curriculum.com/

Stop typing sudo before every command! sudo usermod -aG docker <yourUserName>

List running docker containers: docker ps

List all docker containers (including inactive): docker ps -a

Enter interactive shell in container: docker exec -it <container name> /bin/bash

Find IP of running docker container: docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <containerName>

Find container id: docker commit -p [containerId] [ContainerName]

Back up docker container: docker save -o [path/to/outputfile.tar] [ContainerName]

Load docker container: docker load -i [path/to/outputfile.tar]

View docker images (including backups): docker images

Purge inactive docker containers: docker container prune

Purge inactive docker containers AND images (windows powershell): docker container prune ; docker rmi $(docker images -q -f dangling=true)

Build and run a docker project (using test project librarium as an example): docker build -t spideyclick/librarium . && docker run --rm --name librarium -p 8000:8000 spideyclick/librarium

How to publish a project

Start by downloading a template. It's best to start from the official images at first.

Download the image: docker pull <imageName>

Start a container: docker run --name <projectName> -p 8000:8000 <imageName>

Run the image in a container, make your changes, then run docker commit <containerId> <imageName> (i.e. docker commit e4dc459c63a9 spideyclick/librarium)

To publish: docker push spideyclick/librarium

You may need to authenticate first: docker login

Sample Dockerfile

This is a Django project example

FROM python:3


WORKDIR /usr/src/app
COPY . .


RUN pip install django
RUN pip install pytz
RUN python ./Librarium/manage.py makemigrations
RUN python ./Librarium/manage.py migrate


STOPSIGNAL SIGTERM


CMD ["python", "./Librarium/manage.py", "runserver", "0.0.0.0:8000"]

Contact host without joining host network

https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container

Technically a docker container can contact the host just by going to its gateway. Makes sense. You can just do a docker inspect <yourcontainer> to get the gateway address, then from within docker you can use that IP to curl or do AJAX requests, etc.

The address seems to be able to change from container to container. I think it's assigned when the container is created.