
$ sudo docker container run -it -v $(pwd)/share-data:/home alpine $ echo "Hello linoxide readers" > share-data/test.txt $ docker container run -v host-machine-location:container-storage imageįor example, let’s mount the ‘share-data’ directory on your host machine into ‘/home’ in the container: $ mkdir share-data/ Let’s use -v option as the follows for mounting volumes: If you want to keep the data in the container persistently, you have to mount it to a shared storage volume. Now, let's use 'curl' to retrieve the content from localhost:8080 $ curl localhost:8080 Mounting volumes of a containerĭata in a container is ephemeral, it is no longer existed when the container is stopped. By publishing ports, you can map the container ports to the ports on your host machine using -p option as follows: $ dock container run -p host-port:container-port imageįor example, to map port 80 of container nginx to port 8080 on the host machine, run: $ sudo docker container run -d -p 8080:80 nginxĬd85a291dab1ff92fa2ee6275446f758baa8322de2b706f7b581a54825142c5b If you want to access it from the outside, let’s open a port. bin/bash $ sudo docker container run -it ubuntu /bin/shīy default, in order to access the process running inside a container, you have to go into inside it.

Using the -it option following with ‘/bin/bash’ or ‘/bin/sh’ to launch the interactive mode, for example: $ sudo docker container run -it ubuntu echo $0 Thanks to this feature, you can execute commands inside a container with a shell.

If you want to run that container in background process (detached mode), let’s use the -d option: $ sudo docker container run -d nginxĤfd8f2933eafaebcc75ef4fe6d0a961f76d8fdbf64383caff7f422a25c60951f Run a container in interactive modeĭocker supports running a container in the interactive mode. docker-entrypoint.sh: Configuration complete ready for start up Run a container in the detached mode docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.shġ0-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/nfġ0-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/nf docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration If the container is still running, it will not return the command prompt.įor example: $ sudo docker container run nginx It means container process attaches to the terminal session and displays the output. When we working with docker containers, there are two modes of running them: attached mode and detached mode.īy default, Docker runs the container in foreground. If you want to explicitly assign a name for your container, let’s run the command with the following syntax: $ docker container run -name container-name image-nameįor example: $ sudo docker container run -name hello-linoxide hello-worldĬONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESĮb2f0c9cc658 hello-world "/hello" 21 seconds ago Exited (0) 20 seconds ago hello-linoxide Output: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESĩ41b67537bbb ubuntu "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago musing_elgamalĪ7b197b56d2c bf756fb1ae65 "/hello" About a minute ago Exited (0) About a minute ago determined_faradayĠb40e331161e hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago nervous_sammet When you run a container with the basic syntax ‘docker container run’, the Docker will randomly generate a name for each container as you can see in the below: $ sudo docker container ls -a It's recommended to preface the docker run command with sudo or the other workaround without sudo. Note: From the Docker version 1.13, due to the new syntax of its release, we use ‘docker container run’ instead of ‘docker run’. This message shows that your installation appears to be working correctly. For example: $ sudo docker run bf756fb1ae65 The next time you run that image, it is faster than the first one and you can use the IMAGE-ID instead of image name. Hello-world latest bf756fb1ae65 7 months ago 13.3kB Truongnh1992/go latest 57b4af3567ac 3 months ago 7.41MB

Nginx latest 4bb46517cac3 2 weeks ago 133MBĪlpine latest a24bb4013296 3 months ago 5.57MB Ubuntu latest 4e2eef94cd6b 9 days ago 73.9MB Output: REPOSITORY TAG IMAGE ID CREATED SIZE
