Docker PythonDev

Overview

This is initially written while looking over Python classes but most of this article can be reused for other langages.

First Sample

docker run -it --name myflask1 -p 5000:5000 -v ${PWD}:/app python:3.7 bash

${PWD} : links the current folder in Terminal to the /app folder in the docker image bash : opens a bash prompt on the image. type exit to get out. in the command prompt use python to open the python command prompt. Check version and use exit() to get out.

docker run -it –name jupyter-data1 -p 8888:8888 -v ${PWD}:/home/jovyan/work -e JUPYTER_ENABLE_LAB=yes jupyter/datascience-notebook –ip=0.0.0.0 –port=8888

when existing the container or stopping it to restart: docker start -ia jupyter-data1

Interactive containers

docker run -it –name fhapy python:3 bash Creates a Python container named fhapy and launches the bash command -it makes it interactive and allows to itneract with the bash command

–tty creates a terminal session in the container and leaves it open to get logs and all console i/o

CTRL-C to stop the container CTRL-Q CTRL-P to disconnect the session

To reconnect, use the attach command docker attach fhapy

Port Mapping

Can only be set at docker creation. If port mapping is incorrect, the container must be removed and recreated with proper values.

docker run -it –name fhanginx -p8080:80 -p:8443:443 nginx bash

Creates a NGinx docker container named fhanginx then links local host port 8080 to docker port 80, local host port 8443 to container port 443.

Bind Mount Host folders

docker run -it –name fhanginx -p8080:80 -p:8443:443 -v ${PWD}:/html nginx bash

Several -v can be used to bind several directories. ${PWD} represent the local directory of the local host, where the docker command is issued. ${PWD}/subfolder to add content of a local subdirectory. ` can be used in path to add a space in the folder.

List containers

docker container ls docker ps

docker rename oldname newname

BAckground mode

docker run -d –name fhanginx -p8080:80 -p:8443:443 -v ${PWD}:/html nginx

-d runs in background

Logs

docker container logs fhanginx docker logs fhanginx shows logs from container fhanginx

docker logs fhanginx –since=1h shows logs from the last hour

-f shows logs in real time -t shows time frames

Container operations

docker container stop fhanginx docker stop fhanginx Stops container fhanginx gracefully

docker kill fhanginx Shutdowns container immediately.

docker container create … docker create … Same as docker run without starting the container

docker container start containername docker start containername Starts a container (created or stopped)

-a : attach to container stdoutput (output) -i : attach container stdin (interactive) -ai : both

docker inspect fhanginx shows all infos about the contianer

docker container rm [-f] containernmame docker container rm [-f] containerid docker rm [-f] containernmame docker rm [-f] containerid Removes containers by name or id

docker container prune Removes all stopped containers

Docker Images operations

docker image pull image docker pull image

Pulls image to local registry (if no version specified uses :latest by default)

docker pull gcr.io/google-containers/python:3.5.1-slim Pulls image from google repository instead of default hub.docker.com

docker image tag image newname docker tag image newname

Add new names to an existing image. docker tag gcr.io/google-containers/python:3.5.1-slim python:gcr

docker rmi : remove image name.

docker image ls [imagename] docker images [imagename] List all or specific images in the registry

docker image rm imagename docker rmi imagename Removes image “imagename” from the registry

docker image prune -a -f Removes all unused images. -f removes confirmation request

docker image inspect / history Shows details of the image configuration and history of commands ran in the image. Good source of info.

–entrypoint : To override behaviour of containers by overriding the default entrypoint. To investigate further.

docker run -e VAR=var …. Sets variable values. docker run –env-file file … provides a file with multiple environment values set to specific values.

docker run … env env will output all the environment variables with their values.