How to Run a Docker Container on Your Local Machine
How to Run a Docker Container on Your Local Machine
This quick walkthrough will show you how to dockerize and run an Angular2 app in a container on your local machine, with frequently used Docker commands.
Join the DZone community and get the full member experience.
Join For FreeDiscover a centralized approach to monitor your virtual infrastructure, on-premise IT environment, and cloud infrastructure – all on a single platform.
I have been working on a hobby project, Nutrition Tracking App, to practice the latest Continuous Integration tools. This post is about dockerizing an Angular2 app and on your local machine. I skipped the basic definitions about Docker, containers, and CI for the sake of simplicity.
Docker Commands
Frequently used Docker commands:
Build Docker Image
docker build -t image-name .
Run Docker Image
docker run -p 80:80 -it image-name
Stop All Docker Containers
docker stop $(docker ps -a -q)
Remove All Docker Containers
docker rm $(docker ps -a -q)
Remove All Docker Images
docker rmi $(docker images -q)
Port Bindings of a Specific Container
docker inspect [container-id]
Dockerfile
A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Here's a simple Dockerfile that copies the "dist" folder to /usr/share/nginx/html:
FROM nginx
COPY dist /usr/share/nginx/html
EXPOSE 80
Project Structure
The Dockerfile should be located in the root of the project. Here is an example from my project:
How to Run the Docker Container on Your Local Machine
Build
docker build -t angular-webpack .
Run
docker run -p 9000:80 -t angular-webpack
This will create a container with the image "angular-webpack" and bind the container’s port 80 to the host machine’s port 9000. After the "docker run -p 9000:80 -it angular-webpack" command, the Docker container runs on a Linux virtual machine. So we can't run Docker natively on Windows or a Mac. The following properties must be set:
Set Environment Properties
set USERPROFILE = C:\Users\xxx --> set your user proile
set DOCKER_CERT_PATH=%USERPROFILE%\.docker\machine\machines\default
set DOCKER_HOST=tcp://192.168.99.100 --> find this IP in Docker Quick Start terminal
set DOCKER_MACHINE_NAME=default
set DOCKER_TLS_VERIFY=1
Test: http://192.168.99.100:9000/index.html
For troubleshooting: https://stackoverflow.com/questions/41208782/docker-localhost-process-not-working-on-windows.
Learn how to auto-discover your containers and monitor their performance, capture Docker host and container metrics to allocate host resources, and provision containers.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}