If you want your container (that has Tensorflow already preinstalled, since it is running from the Tensorflow image) to access your script, you need to mount that script from your host onto a local path in your container.
docker run - v / path / to / your / script: /path/to / script
The
-v
flag can also be used to mount a single file - instead of just directories - from the host machine.
$ docker run--rm - it - v~/.bash_history:/.bash_history ubuntu / bin / bash
Last updated 2021-01-28 UTC.
You can use multiple variants at once. For example, the following downloads TensorFlow release images to your machine:
docker pull tensorflow / tensorflow # latest stable release docker pull tensorflow / tensorflow: devel - gpu # nightly dev release w / GPU support docker pull tensorflow / tensorflow: latest - gpu - jupyter # latest release w / GPU support and Jupyter
To start a TensorFlow-configured container, use the following command form:
docker run[-it][--rm][-p hostPort: containerPort] tensorflow / tensorflow[: tag][command]
Let's verify the TensorFlow installation using the latest
tagged image. Docker
downloads a new TensorFlow image the first time it is run:
docker run - it--rm tensorflow / tensorflow\
python - c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
To run a TensorFlow program developed on the host machine within a container,
mount the host directory and change the container's working directory
(-v hostDir:containerDir -w workDir
):
docker run - it--rm - v $PWD: /tmp -w /tmp
tensorflow / tensorflow python. / script.py
Start a Jupyter Notebook server using TensorFlow's nightly build:
docker run - it - p 8888: 8888 tensorflow / tensorflow: nightly - jupyter
Updated July 21st, 2022
After downloading and install Docker via the respective installers, run the command below in a terminal/command prompt to confirm that Docker has been successfully installed:
docker run hello - world
which should output :
Unable to find image‘ hello - world: latest’ locally
latest: Pulling from library / hello - world
0e03 bdcc26d7: Pull complete
Digest: sha256: 4 cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
Status: Downloaded newer image
for hello - world: latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the“ hello - world” image from the Docker Hub.(amd64)
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
In your terminal run the following command below:
docker pull tensorflow / serving
Step 2: In the project folder, create a new script called model.py, and paste the code below:
import matplotlib.pyplot as plt import time from numpy import asarray from numpy import unique from numpy import argmax from tensorflow.keras.datasets.mnist import load_data from tensorflow.keras import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import Conv2D from tensorflow.keras.layers import MaxPool2D from tensorflow.keras.layers import Flatten from tensorflow.keras.layers import Dropout #load MNIST dataset (x_train, y_train), (x_test, y_test) = load_data() print(f 'Train: X={x_train.shape}, y={y_train.shape}') print(f 'Test: X={x_test.shape}, y={y_test.shape}') # reshape data to have a single channel x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)) x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)) # normalize pixel values x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 # set input image shape input_shape = x_train.shape[1: ] # set number of classes n_classes = len(unique(y_train)) # define model model = Sequential() model.add(Conv2D(64, (3, 3), activation = 'relu', input_shape = input_shape)) model.add(MaxPool2D((2, 2))) model.add(Conv2D(32, (3, 3), activation = 'relu')) model.add(MaxPool2D((2, 2))) model.add(Flatten()) model.add(Dense(50, activation = 'relu')) model.add(Dropout(0.5)) model.add(Dense(n_classes, activation = 'softmax')) # define loss and optimizer model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['accuracy']) # fit the model model.fit(x_train, y_train, epochs = 10, batch_size = 128, verbose = 1) # evaluate the model loss, acc = model.evaluate(x_test, y_test, verbose = 0) print('Accuracy: %.3f' % acc) #save model ts = int(time.time()) file_path = f "./img_classifier/{ts}/" model.save(filepath = file_path, save_format = 'tf')
You can inspect the saved model in the folder. It should be similar to the one shown below:
├── img_classifier│├── 1600788643││├── assets││├── saved_model.pb││└── variables