Docker Basic Example
Example: Run python app
Alternative: Dockerizing a Jupyter Notebook (with repo2docker)
Basic process:
- Create Dockerfile
-
Run
docker build .which creates an image based on the
Dockerfilein.with context..Alternative: With Docker file (
-f) flag:docker build -f /path/to/a/Dockerfile .
In Detail:
-
Create Dockerfile, e.g here an example
Dockerfile:Dockerfile# our base image (a linux distribution) FROM alpine:latest # Install python and pip RUN apk add --update py-pip # install Python modules needed by the Python app COPY requirements.txt /usr/src/app/ RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt # copy files required for the app to run COPY app.py /usr/src/app/ COPY templates/index.html /usr/src/app/templates/ # tell the port number the container should expose EXPOSE 5000 # run the application CMD ["python", "/usr/src/app/app.py"]WORKDIRsets the directory from which all following commands will be exectued by default. -
Build (with tag flag (
-t) to create a name for your image with a tag of the formname:tag)docker build -t andrusch/myfirstapp . -
Run
docker run -p 8888:5000 --name myfirstapp andrusch/myfirstapp
Here in 8888:5000, 8888 stands for the external port you can use outside of the docker container (e.g. in your browser or via Insomnia/Postman) and 5000 for the internal port which is open in the container.
- Visit
http://192.168.99.100:8888/,192.168.99.100was found using$ docker-machine ip docker-default. -
Stop and remove container when done
$ docker stop myfirstapp $ docker rm myfirstapp
Example: Run apache container
To run a basic php app with apache:
-
Create a simple Dockerfile which pulls the tutum/apache-php image
DockerfileFROM tutum/apache-php RUN rm -fr /app ADD . /app -
Build
docker build -t my-name/my-app . -
Run
docker run -p 4545:80 my-name/my-app
Example: Run nginx server
-
Create
DockerfileDockerfileFROM alpine:3.16.0 RUN apk update RUN apk add nginx ENTRYPOINT ["nginx", "-g", "daemon off;"] -
Build image
docker build -t my-nginx . -
Run container: Creates and runs the container
docker run -it -p 8081:80 --name super-duper2 my-nginx -
Go to
http://localhost:8081on you system.If you see the following it was a success:
Discuss on Twitter ● Improve this article: Edit on GitHub