Docker notes #2
We continue to learn Docker. See the first part on the learning notes.
docker tags
It works like git tags.
docker pull python
Will fetch the python:latest where latest is the default tag. I prefer alpine and python3.6, so if I want to use it:
docker pull python:alpine3-6
If I want another version of python, I can use corresponding tags in the python repository.
How to access the running web app inside the container?
Two ways.
- Use the container's internal network IP to access the web server inside the container. (only accessible to docker host)
To learn the IP:
docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_id>
inspect command returns lots of useful information about the container.
- Port mapping: This maps the hosts :80 to container's :5000.
docker run -p 80:5000 <container_name>
Volume mapping
Use case: Keeping databases persisted.
docker run -v /host/data_dir:/container/var/lib/mysql <container_name>
Image History
docker history <container>
Let's see how python:alpine3-6 is built:
(lightsteem-test) ➜ transmitter git:(master) ✗ docker history python:alpine3.6
IMAGE CREATED CREATED BY SIZE COMMENT
37daba746bbe 3 months ago /bin/sh -c #(nop) CMD ["python3"] 0B
<missing> 3 months ago /bin/sh -c set -ex; apk add --no-cache --v… 6.5MB
<missing> 3 months ago /bin/sh -c #(nop) ENV PYTHON_PIP_VERSION=10… 0B
<missing> 3 months ago /bin/sh -c cd /usr/local/bin && ln -s idle3… 32B
<missing> 3 months ago /bin/sh -c set -ex && apk add --no-cache --… 75.8MB
<missing> 3 months ago /bin/sh -c #(nop) ENV PYTHON_VERSION=3.6.6 0B
<missing> 4 months ago /bin/sh -c #(nop) ENV GPG_KEY=0D96DF4D4110E… 0B
<missing> 4 months ago /bin/sh -c apk add --no-cache ca-certificates 633kB
<missing> 4 months ago /bin/sh -c #(nop) ENV LANG=C.UTF-8 0B
<missing> 4 months ago /bin/sh -c #(nop) ENV PATH=/usr/local/bin:/… 0B
<missing> 9 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 9 months ago /bin/sh -c #(nop) ADD file:6edc55fb54ec9fc36… 3.97MB
Pushing your image to docker hub
- First, log in with
docker login
.
docker push <yourusername>/<imagename>
Looks like the free plan on docker hub has one private repository access.
Writing Dockerfiles
An example Dockerfile for transmitter project:
FROM python:alpine3.6
RUN apk update
RUN apk add --no-cache gcc python3-dev libc-dev libressl-dev
COPY . /app
RUN mkdir -v '/root/.transmitter/'
RUN mv -v /app/config.json.docker /root/.transmitter/config.json
WORKDIR /app
RUN pip install -e .
ENTRYPOINT ["transmitter"]
- Use a python3.6 image
- Update the packages
- Install the related packages
- Copy our app to the container
- Create a config dir and copy the example config here.
- Install the app via pip.
ENTRYPOINT
is the command and params will be executed ondocker run
.
So, if a user builds the transmitter's Dockerfile and run it, transmitter command will run.
Arguments are automatically passed. Ex: docker run -t transmitter enable
to enable your witness.
We have covered the CLI and the important commands. I will be learning docker compose and docker swarm in the next days.
What have we here,one year !BEER
View or trade
BEER
.Hey @emrebeyler, here is a little bit of
BEER
for you. Enjoy it!