Sitemap
Jack Pan

Jack Pan

Docker Note

2 min readApr 14, 2018

--

Build and Run

Dockerfile

# Use an official Python runtime as a parent image
FROM python:2.7-slim
# 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 pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]

And build using -t so it has a friendly name

docker build -t friendlyhello .

ls to list images

$ docker image lsREPOSITORY            TAG                 IMAGE ID
friendlyhello latest 326387cea398

mapping your machine’s port 4000 to the container’s published port 80 using -p

docker run -p 4000:80 friendlyhello

Get Jack Pan’s stories in your inbox

Join Medium for free to get updates from this writer.

run the app in the background, in detached mode:

docker run -d -p 4000:80 friendlyhello

show and stop container

docker container lsdocker container stop 1fa4ab2cf395

Share image

The notation for associating a local image with a repository on a registry is username/repository:tag. The tag is optional, but recommended, since it is the mechanism that registries use to give Docker images a version. Give the repository and tag meaningful names for the context, such as get-started:part2. This puts the image in the get-started repository and tag it as part2.

docker tag friendlyhello john/get-started:part2$ docker image lsREPOSITORY               TAG                 IMAGE ID       ...
friendlyhello latest d9e555c53008 ...
john/get-started part2 d9e555c53008 ...

So, it will create a new image which can be pushed to the registry later.

docker push username/repository:tag

And you can run it by

docker run -p 4000:80 username/repository:tag

If the image isn’t available locally on the machine, Docker pulls it from the repository.

Cheatsheet

docker run -it

run in interactive mode

--

--