How to run a container
- $ docker run repository/(imageName):tag any-command-to-run-when-spun-up
- Creates the container, spins it up and runs it
- :1.24 → how you specify a version of the image
- Example: $ docker run busybox:1.24 echo “hello world”
- Example2: $ docker run busybox:1.24 ls /dev (list files in /dev)
- Above command puts you inside containers console.
- To free up console, run -d flag (“detached” mode)
- $ docker run repository/(imageName):tag any-command-to-run-when-spun-up
Flags - Runs container w/specified parameters
- Interactive
- -i → interactive
- -t → creates psuedo TTY with stdin & stdout
- TTY = TeleType Writer (originally for deaf people) tele comes from a Greek root meaning far or distant. TTY's provide a virtual interface similar to what the physical machines provided. This is the origin of the 80 char width and carriage return
- Example: $ docker run -t -i busybox:1.24
- Gives you terminal inside the container. Prompt looks like / #
- Exit terminal in container
- Interactive
/ # exit → exits terminal & SHUT DOWN CONTAINER
- Detached
- -d → detached mode. Frees up console
- Interactive mode
- $ docker exec -it <cont_id> command
- exec lets you run a command inside a running container
- -it → enter container in interactive mode
- bash → command (enter bash mode)
- To enter detached container once running …
- Example → $ docker exec -it 175cfac1b48a bash
- See Running programs → $ ps axu (just a shell command)
- Exit interactive mode (& return to daemon mode) → ctrl-p & ctrl-q
- exec lets you run a command inside a running container
- $ docker exec -it <cont_id> command
- Detached
“Hitting” IP address of a container
- We linked external port to the DM’s internal port when we created the container
- docker run -p 8080:3000 imageName - (machine port:container port)
(DM external port:DM internal port)
- $ docker-machine ls to get ip of docker_host environment
- $ docker ps to get ip of specific container
- dockerapp_1 | * Running on http://0.0.0.0:5000/
This is the ip WITHIN container. Need to get to where container is running (which is the ip of docker_host)
- Therefore ip of running container is:
Docker_host_ip:external port exposed by container
Examples of finding docker_host IP:container_external_port
- Node example → $ docker run -p 8080:3000 node
- Remember, whale icon: “docker is configured to use the default machine with IP 192.168.99.100”. This is host ip
- Port can be ‘hit’ outside linux VM at this ip + port #
- Can hit port within Linux VM at http://localhost:3000
- To hit port from outside the Linux VM, we set external port to 8080 so container will export on Linux VM’s ip address on port 8080 http://192.168.99.100:8080
- Tomcat example → $ docker run -p 8888:8080 tomcat:8.0
- Node example → $ docker run -p 8080:3000 node
$ docker-machine ls → Lists DMs & host-ip