Build and run your sample app with Docker (demo)
Whatever you start to use, it’s always beneficial to have a look at the official documentation before going so fast by blindly copy&paste stack overflow. And, of course, Docker is not an exception. Great is that it has nice documentation for us.
Four months after graduating from programming boot camp, I first created and pushed docker images following the doc.
I’m gonna share the procedure in this post.
Configurations
System Version: macOS 10.14.3 (18D109)
Kernel Version: Darwin 18.2.0
Shell: zsh
Package manager: Homebrew
TL;DR
If you haven’t installed Docker, follow this article and install it first.
$ docker-machine start default
$ docker-machine ip
->192.168.99.101 (check your ip)$ mkdir sample && cd ./sample
$ touch Dockerfile app.py requirements.txt[...edit file...]$ docker build -t sample
$ docker run -d -p 4000:80 sample[open http://192.168.99.101:4000/]$ docker tag sample [username]/[repository]:[tag]
$ docker push [username]/[repository]:[tag]
$ docker run -d -p 4000:80 [username]/[repository]:[tag]$ touch docker-compose.yml
$ docker swarm init --advertise-addr [192.168.99.101]
$ docker stack deploy -c docker-compose.yml sample$ docker service ps sample_web# Take down the app and swarm
$ docker stack rm sample
$ docker swarm leave --force
Step1. Build, Run and Share a single container
Create working directory
Before developing your app, make sure:
- Docker machine is active (check IP address)
- You are in your working directory
If you haven’t made it, make “sample” directory for instance.
$ docker-machine start default
$ docker-machine ip
->192.168.99.101 (check your ip)$ mkdir sample && cd ./sample
$ touch Dockerfile app.py requirements.txt
Develop your app
Here is a sample source code for each file.
If you want to create app yourself, don’t forget to make sure the Dockerfile is configured so the external libraries will be installed to make your app work.
Dockerfile
app.py
requirement.txt
Build Docker image
To build your docker image, run:
$ docker build -t sample
-t
name your Docker image (“sample” in this case)
⚠️ Every time you modify the source code of your app, run the command above.
# check your image is created
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
sample latest 6ed40d05b72b 2 hours ago 131MB
python 2.7-slim 48e3247f2a19 3 weeks ago 120MB
Check the app locally
Once you build the docker image, run that image.
$ docker run -d -p 4000:80 sample
-p
map your laptop’s port 4000 to the container’s exposed port 80 (defined in Dockerfile)-d
detached mode
Open “http://[IP address]:4000/” in your browser. (✅check your Docker machine IP address, 192.168.99.101 in my case)
If you see like this, your first docker app is working well!
Upload your image to Docker hub
Now it’s time to share your image to the public.
First, associate your local image to the docker hub repository.
$ docker tag [image] [username]/[repository]:[tag]
- [image]: name of your local image to share
- [username]: your Docker account
- [repository]: docker hub repository to connect
- [tag]: tag name in the docker hub (optional, but officially recommended)
$ docker tag sample yutafujii/sample:ver1.0# check your repo image is created
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
sample latest 6ed40d05b72b 2 hours ago 131MB
python 2.7-slim 48e3247f2a19 3 weeks ago 120MB
yutafujii/sample ver1.0 40b72bd05f23 2 hours ago 131MB
Push the image
To push your image in local to remote, which means docker hub, run the following command:
$ docker push [username]/[repository]:[tag]# in this case, for instance
$ docker push yutafujii/sample:ver1.1
Pull and run the remote image
To pull an image from a remote repository, docker run
with the repository name passed:
$ docker run -d -p 4000:80 yutafujii/sample:ver1.1
Step2. Organize multiple containers
Imagine when you are to build a huge application. There will be multiple features. One of them will receive a HTTP request and return JSON after fetching data, while another will receive that JSON and render with front-end language. And so on…
Then it’s quite reasonable to split the containers into each specific features (Who does want to install Python in Vue.js framework application?).
So you will create multiple images, but how to organize them as a whole application that only works when collaborated together?
Here the concept of services and ‘docker-compose.yml’ file comes in.
- a service is just an alias of container in production
- docker-compose.yml file organize containers (services)
Prerequisite
You need to run this command to make this:
$ docker swarm init --advertise-addr 192.168.99.101
(Replace IP address to your docker machine IP. cf docker-machine ip
)
Compose containers
Create and edit docker-compose.yml
file. You can put this file wherever you want.
As a boilerplate, Docker documentation instruct as follows:
docker-compose.yml
For a real situation, you’ll define second, third services that make the whole app work well.
Deploy services
To deploy your services, just run the following command:
# deploy your app using docker-compose.yml file
$ docker stack deploy -c docker-compose.yml sample
Then you’ll also see your tasks that are working for the service by
# list the tasks for your service:
$ docker service ps sample_web
Take down the app and the swarm
When you want to stop running stacks and swarms, remove them by the following command:
# Take down the app and swarm
$ docker stack rm sample
$ docker swarm leave --force