Skip to content

Instantly share code, notes, and snippets.

@edwilliams
Created September 17, 2021 08:57
Show Gist options
  • Select an option

  • Save edwilliams/e2072df9ce1c90ae82212b3c61ba124a to your computer and use it in GitHub Desktop.

Select an option

Save edwilliams/e2072df9ce1c90ae82212b3c61ba124a to your computer and use it in GitHub Desktop.

Docker

Create folder with...

  • server.js
const http = require('http')

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('Hello World\n')
})

server.listen(4000)

console.log('Server running at http://localhost:4000')
  • package.json
{
  "scripts": {
    "start": "node server.js"
  }
}
  • Dockerfile
FROM node:16.6.1
COPY . /src
RUN cd /src && npm install
EXPOSE 4000
CMD ["node", "/src/server.js"]

Build the Docker image: docker build -t hello-world .

Run the image in a container: docker run -d -p 4001:4000 hello-world

The -d flag says to run the container in the background (daemon mode). The -p flag maps port 4000 from the container to port 4001 on the docker machine.

View your new container: docker ps -a

Check the logs for your container: docker logs <container-id> Check the port of the container: docker port <container-id>

Open the app running on the docker machine: open http://locahost:4001

Notes

If you make changes to your application, you will need to rebuild your image and restart your container.

The docker-machine command controls the virtual machine that is running Docker on your machine.

View logs for a docker container: docker logs <container-id>

List the running containers: docker ps -a

List all local images: docker images

Remove an image: docker rmi <image-id>

Remove a container: docker rm <container-id>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment